package com.arcvideo.acsplayer.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.config.PlayConfig.AMMF_STATE;
import com.arcvideo.acsplayer.config.PlayConfig.SCREEN_STATE;
import com.arcvideo.acsplayer.utils.DateUtil;

public class PlayControlView extends FrameLayout {

	private Context mContext = null;
	private ImageButton mControlPlayPauseButton = null;
	private ImageButton mControlNextButton = null;
	private SeekBar mControlSeekBar = null;
	private ImageButton mControlDualButton = null;
	private ImageButton mControlGyroButton = null;
	private ImageButton mControlPlayAgainButton = null;
	private ImageButton mControlFullScreenButton = null;
	private Button mControlHDButton = null;
	private TextView mControlLeftTimeText = null;
	private TextView mControlRightTimeText = null;

	private int mRotationType = 0;
	private boolean mbVRMode = false;
	private int mCurrentPosition = 0;
	private int mDuration = 0;

	private PlayControlInterface mPlayControlInterface = null;

	/**
	 * 回调函数
	 *
	 * @author skl5250
	 *
	 */
	public interface PlayControlInterface {
		public void clickPlay();

		public void clickNext();

		public void clickDual();

		public void clickGyro();

		public void clickHD();

		public void clickFullScreen();

		public void clickSeek(int currentPosition);

	}

	/**
	 * 进度条事件
	 */
	private OnSeekBarChangeListener mSeekBarChangeListener = new OnSeekBarChangeListener() {

		// 触发操作，拖动
		public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
			mCurrentPosition = progress;
			mControlLeftTimeText.setText(DateUtil.getTimeCount(progress));
			// mPlayControlInterface.clickSeek(progress);
		}

		// 表示进度条刚开始拖动，开始拖动时候触发的操作
		public void onStartTrackingTouch(SeekBar seekBar) {
		}

		// 停止拖动时候
		public void onStopTrackingTouch(SeekBar seekBar) {
			mCurrentPosition = seekBar.getProgress();
			mControlLeftTimeText.setText(DateUtil.getTimeCount(mCurrentPosition));
			mPlayControlInterface.clickSeek(mCurrentPosition);
		}

	};

	/**
	 * 按键点击事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.play :
					mPlayControlInterface.clickPlay();
					break;
				case R.id.next :
					mPlayControlInterface.clickNext();
					break;
				case R.id.gyro :
					mPlayControlInterface.clickGyro();
					break;
				case R.id.dual :
					mPlayControlInterface.clickDual();
					break;
				case R.id.hd :
					mPlayControlInterface.clickHD();
					break;
				case R.id.full_screen :
					mPlayControlInterface.clickFullScreen();
					break;
				default :
					break;
			}
		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public PlayControlView(Context context) {
		super(context);
		mContext = context;
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public PlayControlView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param isVRMode
	 * @param playControlInterface
	 */
	public void init(int rotationType, boolean isVRMode, PlayControlInterface playControlInterface) {
		mRotationType = rotationType;
		mbVRMode = isVRMode;
		mPlayControlInterface = playControlInterface;
		initUI();
	}

	/**
	 * 初始化UI
	 */
	public void initUI() {
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_play_control, this);
		mControlPlayPauseButton = (ImageButton) findViewById(R.id.play);
		mControlPlayPauseButton.setOnClickListener(mOnClickListener);
		mControlNextButton = (ImageButton) findViewById(R.id.next);
		mControlNextButton.setOnClickListener(mOnClickListener);
		mControlSeekBar = (SeekBar) findViewById(R.id.play_seekbar);
		mControlSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener);
		mControlGyroButton = (ImageButton) findViewById(R.id.gyro);
		mControlGyroButton.setOnClickListener(mOnClickListener);
		mControlHDButton = (Button) findViewById(R.id.hd);
		mControlHDButton.setOnClickListener(mOnClickListener);
		mControlDualButton = (ImageButton) findViewById(R.id.dual);
		mControlDualButton.setOnClickListener(mOnClickListener);
		mControlFullScreenButton = (ImageButton) findViewById(R.id.full_screen);
		mControlFullScreenButton.setOnClickListener(mOnClickListener);
		mControlLeftTimeText = (TextView) findViewById(R.id.play_time_left);
		mControlRightTimeText = (TextView) findViewById(R.id.play_time_right);

		reload(mRotationType, mbVRMode);
	}

	/**
	 * 重新加载
	 *
	 * @param rotationType
	 * @param isVRMode
	 */
	public void reload(int rotationType, boolean isVRMode) {

		mRotationType = rotationType;
		mbVRMode = isVRMode;

		if (mbVRMode) {
			mControlGyroButton.setVisibility(VISIBLE);
			mControlHDButton.setVisibility(VISIBLE);
			mControlDualButton.setVisibility(VISIBLE);
			mControlFullScreenButton.setVisibility(GONE);

			RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mControlSeekBar.getLayoutParams();
			lp.addRule(RelativeLayout.LEFT_OF, R.id.dual);
			lp.rightMargin = 20;
			mControlSeekBar.setLayoutParams(lp);
		} else {
			if (mRotationType == Configuration.ORIENTATION_LANDSCAPE) {
				mControlHDButton.setVisibility(View.VISIBLE);
				mControlGyroButton.setVisibility(GONE);
				mControlDualButton.setVisibility(GONE);
				mControlFullScreenButton.setVisibility(View.GONE);

				RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mControlSeekBar.getLayoutParams();
				lp.addRule(RelativeLayout.LEFT_OF, R.id.hd);
				lp.rightMargin = 20;
				mControlSeekBar.setLayoutParams(lp);
			} else if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mControlHDButton.setVisibility(View.GONE);
				mControlGyroButton.setVisibility(GONE);
				mControlDualButton.setVisibility(GONE);
				mControlFullScreenButton.setVisibility(View.VISIBLE);

				RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mControlSeekBar.getLayoutParams();
				lp.addRule(RelativeLayout.LEFT_OF, R.id.full_screen);
				lp.rightMargin = 20;
				mControlSeekBar.setLayoutParams(lp);
			}
		}

	}

	/**
	 * 设置总播放进度
	 *
	 * @param duration
	 */
	public void setDuration(int duration) {
		mDuration = duration;
		mControlRightTimeText.setText(DateUtil.getTimeCount(mDuration));
		mControlSeekBar.setMax(mDuration);
	}

	/**
	 * 设置步进进度
	 *
	 * @param increment
	 */
	public void setKeyProgressIncrement(int increment) {
		mControlSeekBar.setKeyProgressIncrement(increment);
	}

	/**
	 * 设置当前进度
	 *
	 * @param currentPosition
	 */
	public void setCurrentPosition(int currentPosition) {
		mCurrentPosition = currentPosition;
		mControlSeekBar.setProgress(mCurrentPosition);
		mControlLeftTimeText.setText(DateUtil.getTimeCount(mCurrentPosition));
	}

	/**
	 * 设置播放状态
	 *
	 * @param playState
	 */
	public void setPlayState(AMMF_STATE playState) {
		if (playState == AMMF_STATE.STOPPED || playState == AMMF_STATE.PAUSED) {
			mControlPlayPauseButton.setBackgroundResource(R.drawable.btn_play);
		} else if (playState == AMMF_STATE.STARTED) {
			mControlPlayPauseButton.setBackgroundResource(R.drawable.btn_stop);
		} else {
			mControlPlayPauseButton.setBackgroundResource(R.drawable.btn_play);
		}
	}

	/**
	 * 设置画质状态
	 *
	 * @param state
	 */
	public void setQualityState(String state) {
		if (HDselectView.STATE_4K.equals(state))
			mControlHDButton.setText(R.string.clarity_4K);
		else if (HDselectView.STATE_ULTRA_CLEAR.equals(state))
			mControlHDButton.setText(R.string.clarity_ultra_clear);
		else if (HDselectView.STATE_HD.equals(state))
			mControlHDButton.setText(R.string.clarity_hd);
		else if (HDselectView.STATE_SD.equals(state))
			mControlHDButton.setText(R.string.clarity_sd);
		else if (HDselectView.STATE_SMOOTH.equals(state))
			mControlHDButton.setText(R.string.clarity_smooth);
	}

	/**
	 * 设置陀螺仪状态
	 *
	 * @param isOn
	 */
	public void setGyroState(boolean isOn) {
		if (isOn) {
			mControlGyroButton.setBackgroundResource(R.drawable.btn_gyro_p);
		} else {
			mControlGyroButton.setBackgroundResource(R.drawable.btn_gyro_n);
		}
	}

	/**
	 * 设置分屏模式
	 *
	 * @param isOn
	 */
	public void setDualState(boolean isOn) {
		if (isOn) {
			mControlDualButton.setBackgroundResource(R.drawable.btn_dual_p);
		} else {
			mControlDualButton.setBackgroundResource(R.drawable.btn_dual_n);
		}
	}

	/**
	 * 设置屏幕模式
	 *
	 * @param state
	 */
	public void setScreenState(SCREEN_STATE state) {
		switch (state) {
			case PORTRAIT_SCREEN :
				mControlFullScreenButton.setBackgroundResource(R.drawable.btn_exit_full_screen_p);
				break;
			case LANDSCAPE_SCREEN :
				mControlFullScreenButton.setBackgroundResource(R.drawable.btn_exit_full_screen_p);
				break;
			default :
				break;
		}
	}

	/**
	 * 进度条增加
	 */
	public void increase() {
		mCurrentPosition++;
		if (mCurrentPosition >= mDuration)
			mCurrentPosition = mDuration;
		mControlSeekBar.setProgress(mCurrentPosition);
		mControlLeftTimeText.setText(DateUtil.getTimeCount(mCurrentPosition));
	}

	/**
	 * 进度条减少
	 */
	public void decrease() {
		mCurrentPosition--;
		if (mCurrentPosition <= 0)
			mCurrentPosition = 0;
		mControlSeekBar.setProgress(mCurrentPosition);
		mControlLeftTimeText.setText(DateUtil.getTimeCount(mCurrentPosition));
	}

	/**
	 * 显示
	 */
	public void show() {
		setVisibility(View.VISIBLE);
		setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_bottom_in));
	}

	/**
	 * 隐藏
	 */
	public void hide() {
		setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_bottom_out));
		setVisibility(View.INVISIBLE);
	}

	/**
	 * 得到播放进度条
	 *
	 * @return
	 */
	public SeekBar getSeekBar() {
		return mControlSeekBar;
	}

}